/*--------------------------------------------------------------------------------
	DESCRIPTION: Creation of lifetime experiences variables for regression 
				 analysis from lifetime experience datasets

--------------------------------------------------------------------------------*/

cap erase "$data_dir/raw/lifeexp_variables_data"
cap mkdir "$data_dir/raw/lifeexp_variables_data"

scalar age = 99

//--------------------------------------------------------
//1) Generate relevant programs
//--------------------------------------------------------

**CREATE TOTAL OBSERVATIONS PROGRAM
capture program drop totalobs
program define totalobs

	args var
	foreach x of varlist `var'_1-`var'_`=scalar(age)' {
		gen d_`x' = 1 if `x'!=.
	}

	cap drop total_`var'_obs*
	egen total_`var'_obs = rowtotal(d_`var'_1-d_`var'_`=scalar(age)'), missing

	drop d_`var'*

end


**CREATE MALMENDIER-NAGEL PROGRAM FROM BIRTH
capture program drop lifeexp
program define lifeexp
	
	args var
	forvalues i = 1/`=scalar(age)' {
	gen weight_`i' = `i' if age>`i' & age!=-99
	}

	forvalues i = 1/`=scalar(age)' {
	gen weighted_`i' = weight_`i'*`var'_`i'
	}
	
	egen sum_weights_1 = rowtotal(weight_1-weight_`=scalar(age)'), missing
	egen w`var'_1 = rowtotal(weighted_1-weighted_`=scalar(age)'), missing
	replace w`var'_1 = w`var'_1/sum_weights_1

	cap drop weighted_* weight_* sum_weights_1

end


**CREATE MALMENDIER-NAGEL PROGRAM FROM BIRTH WITH EXPONENT LAMBDA
capture program drop lifeexp_lambda
program define lifeexp_lambda
	
	args var lambda
	forvalues i = 1/`=scalar(age)' {
	gen weight_`i' = (`i')^`lambda' if age>`i' & age!=-99
	}

	forvalues i = 1/`=scalar(age)' {
	gen weighted_`i' = weight_`i'*`var'_`i'
	}
	
	egen sum_weights_1 = rowtotal(weight_1-weight_`=scalar(age)'), missing
	
	if `lambda'<0 {
		
		if mod(`lambda', 1)==0 {
			local l = -1*`lambda'
			egen w`var'_neg`l' = rowtotal(weighted_1-weighted_`=scalar(age)'), missing
			replace w`var'_neg`l' = w`var'_neg`l'/sum_weights_1
		}
		else {
			local lambda = -1*`lambda'
			local l = round((1-(ceil(`lambda')-`lambda'))*10)
			local k = floor(`lambda')
			egen w`var'_neg`k'_`l' = rowtotal(weighted_1-weighted_`=scalar(age)'), missing
			replace w`var'_neg`k'_`l' = w`var'_neg`k'_`l'/sum_weights_1
		}
	} 
	else {
		if mod(`lambda', 1)==0 {
			egen w`var'_`lambda' = rowtotal(weighted_1-weighted_`=scalar(age)'), missing
			replace w`var'_`lambda' = w`var'_`lambda'/sum_weights_1
		}
		else {
			local l = round((1-(ceil(`lambda')-`lambda'))*10)
			local k = floor(`lambda')
			egen w`var'_`k'_`l' = rowtotal(weighted_1-weighted_`=scalar(age)'), missing
			replace w`var'_`k'_`l' = w`var'_`k'_`l'/sum_weights_1
		}
	}
	cap drop weighted_* weight_* sum_weights_1
end


**CREATE FORMATIVE YEARS PROGRAM
capture program drop formativeyears
program define formativeyears 

	args var
	cap drop fy_`var' pre_fy_`var' post_fy_`var'
	
	egen fy_`var' = rowmean(`var'_18-`var'_25)
	replace fy_`var' = . if `var'_25==. //make missing if haven't even turned 25 yet
	
	cap drop pre_fy_`var'
	egen pre_fy_`var' = rowmean(`var'_1-`var'_17)
	replace pre_fy_`var' = . if `var'_17==. //make missing if haven't even turned 17 yet
	
	cap drop post_fy_`var'
	egen post_fy_`var' = rowmean(`var'_26-`var'_`=scalar(age)')

end


**CREATE PRE-BIRTH PLACEBO PROGRAM
capture program drop prebirth 
program define prebirth 

	args var
	foreach i in 5 10 15 {
		egen preyob_`var'_`i' = rowmean(py_`var'_1-py_`var'_`i')
	}

end


**CREATE STANDARD DEVIATION PROGRAM
capture program drop sd
program define sd

	args var
	egen sd_`var' = rowsd(`var'_1-`var'_`=scalar(age)')

end


**CREATE LIFETIME TOTAL OBSERVATIONS PROGRAM
capture program drop total
program define total

	args var
	egen total_`var' = rowtotal(`var'_1-`var'_`=scalar(age)'), missing

end

//--------------------------------------------------------
//2) Calculate core growth experience variables (MPD)
//--------------------------------------------------------

clear
use "$data_dir/raw/experience_datasets/growth_rate_mpd_exp.dta"

//core growth experience variables
foreach program in totalobs lifeexp formativeyears prebirth {
	`program' growth_rate
}

//total pre-year of birth growth rate observations
foreach x of varlist py_growth_rate_1-py_growth_rate_20 {
	gen d_`x' = 1 if `x'!=.
}
egen total_py_growth_rate_obs = rowtotal(d_py_growth_rate_1-d_py_growth_rate_20), missing
drop d_py_growth_rate*

keep ccode survey_year yob wgrowth_rate_1 ///
	 preyob_growth_rate_5 preyob_growth_rate_10 preyob_growth_rate_15 ///
	 pre_fy_growth_rate fy_growth_rate post_fy_growth_rate ///
	 total_growth_rate_obs total_py_growth_rate_obs
sort ccode survey_year yob

rename yob year
merge m:1 ccode year using "$data_dir/raw/mpd2023_clean.dta", keepusing(ln_gdppc)
drop if _merge==2
drop _merge
rename year yob
rename ln_gdppc yob_ln_gdppc

sort ccode survey_year yob

save "$data_dir/raw/lifeexp_variables_data/growth_rate_mpd_var_exp.dta", replace

//-----------------------------------------------------------------------
//3) Calculate growth experience variables with alternative lambdas (MPD)
//-----------------------------------------------------------------------

clear
use "$data_dir/raw/experience_datasets/growth_rate_mpd_exp.dta"

forvalues k = -3(0.1)-2 {
	lifeexp_lambda growth_rate `k'
}

forvalues k = -2(0.1)-1 {
	lifeexp_lambda growth_rate `k'
}

forvalues k = -1(0.1)-0.1 {
	lifeexp_lambda growth_rate `k'
}

forvalues k = 0(0.1)1 {
	lifeexp_lambda growth_rate `k'
}

forvalues k = 1(0.1)2 {
	lifeexp_lambda growth_rate `k'
}

forvalues k = 2(0.1)3 {
	lifeexp_lambda growth_rate `k'
}

lifeexp_lambda growth_rate 3

drop age growth_rate_* py_growth_rate_*
sort ccode survey_year yob

save "$data_dir/raw/lifeexp_variables_data/growth_mpd_var_lambda_exp.dta", replace

//-----------------------------------------------------------------------
//3) Calculate other experience variables
//-----------------------------------------------------------------------

//MADDISON PROJECT DATABASE (NEGATIVE GROWTH EPISODE, RARE DISASTER, ACCELERATION)
local varlist growth_shock disaster acceleration
foreach y of local varlist {
	clear
	use "$data_dir/raw/experience_datasets/`y'_mpd_exp.dta"
	
	lifeexp `y'
	
	sort ccode survey_year yob
	keep ccode survey_year yob w`y'_1
	
	save "$data_dir/raw/lifeexp_variables_data/`y'_mpd_var_exp.dta", replace
}

//EPIDEMICS, UNREST, EXECUTIVE CHANGES, ECONOMIC CRISES
local varlist prop_affect_epidemic prop_unrest no_exec_changes bank_crisis currency_crisis twin_crisis bank_crisis_w currency_crisis_w twin_crisis_w 
foreach y of local varlist {
	clear
	use "$data_dir/raw/experience_datasets/`y'_exp.dta"
	
	lifeexp `y'
	totalobs `y'
	
	sort ccode survey_year yob
	keep ccode survey_year yob w`y'_1 total_`y'
	
	save "$data_dir/raw/lifeexp_variables_data/`y'_var_exp.dta", replace
}

//INFLATION AND INFLATION VOLATILITY
clear
use "$data_dir/raw/experience_datasets/winsz_inflation_exp.dta"

lifeexp winsz_inflation
sd winsz_inflation
totalobs winsz_inflation

sort ccode survey_year yob
keep ccode survey_year yob wwinsz_inflation_1 sd_winsz_inflation total_winsz_inflation_obs

save "$data_dir/raw/lifeexp_variables_data/winsz_inflation_var_exp.dta", replace

//DEMOCRACY AND INSTITUTIONS
local varlist democracy v2x_polyarchy v2xel_frefair v2x_jucon v2xlg_legcon v2mecenefm v2x_freexp
foreach y of local varlist {
	clear
	use "$data_dir/raw/experience_datasets/`y'_exp.dta"
	
	lifeexp `y'
	
	sort ccode survey_year yob
	keep ccode survey_year yob w`y'_1
	
	save "$data_dir/raw/lifeexp_variables_data/`y'_var_exp.dta", replace
}

//-----------------------------------------------------------
//4) Calculate core growth experience variables (PWT + Barro-Ursua)
//-----------------------------------------------------------

clear
use "$data_dir/raw/experience_datasets/growth_rate_pwt_barro_exp.dta"

//core growth experience variables
foreach program in totalobs lifeexp {
	`program' growth_rate
}

sort ccode survey_year yob

rename yob year
merge m:1 ccode year using "$data_dir/raw/pwt100_barro_clean.dta", keepusing(ln_gdppc)
drop if _merge==2
drop _merge
rename year yob
rename ln_gdppc yob_ln_gdppc

sort ccode survey_year yob

rename total_growth_rate_obs pwtbar_total_growth_rate_obs
rename wgrowth_rate_1 pwtbar_wgrowth_rate_1
rename yob_ln_gdppc pwtbar_yob_ln_gdppc

keep ccode survey_year yob pwtbar_total_growth_rate_obs pwtbar_wgrowth_rate_1 pwtbar_yob_ln_gdppc

save "$data_dir/raw/lifeexp_variables_data/growth_rate_pwt_barro_var_exp.dta", replace
